<?php
/*
 * planning/load-index.phtml
 */
?>
<?php

use RHX\Helper\RHDateTime;
?>
<?php
$data	 = $this->data;
$site	 = $this->site;
?>
<div style="font-size: 0.9em">
    <table class="table table-striped list-table table-condensed">
	<thead>
	    <tr style="text-transform: uppercase; font-weight: bolder;">
		<td width="10%" >Périodes</td>
		<td width="40%" >Semaines</td>
		<td width="25%" >Planning Type</td>
		<td width="25%" >
		    <button class="btn btn-primary btn-small" id="btn-ps-update" data-site="<?php echo $site->id; ?>"><span id="btn-loader"><img src="<?php echo $this->basePath() .'/img/rhx/ajax-waiting.gif' ?>" class="hide"></span><span id="btn-title">Mis à jour des Plannings</span></button>
		</td>
	    </tr>
	</thead>
	<tbody>
	    <?php
	    foreach ($data as $e):
		// Ensemble des variables
		$periode	 = $e['periode'];
		$semaines	 = $e['semaines'];
		$plannings	 = $e['plannings'];
		$countSemaines	 = count($semaines);
		?>
	    <tr>
    		<!-- PERIODE -->
    		<td rowspan="<?php echo $countSemaines; ?>" style="border-bottom: solid 2px #999999">
			<?php echo $periode->libelle; ?>
    		</td>
		    <?php
		    $i = 1;
		    foreach ($semaines as $semaine):
			$borderBottom = false;
			if($i == $countSemaines){
			    $borderBottom = true;
			}
			$planning	 = $plannings[$semaine->id];
			// Traitement temporelle
			$now		 = new RHDateTime('now');
			$lundi		 = new RHDateTime($semaine->lundi);
			$dimanche	 = new RHDateTime($semaine->dimanche);
			// Date en Français
			$lundiFr	 = $lundi->dateFr();
			$dimancheFr	 = $dimanche->dateFr();
			// Editable ou Pas
			$editable	 = true;
			// Si la Aujourd'hui est Supérieur ou égale à Dimanche de la semaine
			if ($now->isEqual($dimanche) || $now->isGT($dimanche)) {
			    // L'édition n'est pas possible
			    $editable = false;
			}
			// Mois de Septembre editable
			$septembre = new RHDateTime('2013-09-22');
	if ($lundi->isGT($septembre)) {
			    // Edition autorisée pour le mois de septembre
			    $editable = true;
			}
			$classNow = '';
			if ($now->isBetween($lundi, $dimanche)) {
			    $classNow = 'periode-now';
			}
			?>
		<!-- SEMAINE -->
                 <?php
                    $noTemplate	 = false;
                    $template	 = null;
                    if (is_null($planning->template))
                        $noTemplate	 = true;
                    else
                        $template	 = $planning->template;
                    //var_dump($template);
                ?>
                
		<td style=" <?php if($borderBottom): ?> border-bottom: solid 2px #999999 ;<?php endif; ?>  color: #005588;" >
                     <?php if ($noTemplate): ?>
	    			<?php echo $semaine->libelle . " : $lundiFr au $dimancheFr" ?>
                     <?php else: ?>
			    <a href="#" class="p<?php echo $periode->id; ?> planning-edit<?php echo " $classNow"; ?>" id="s<?php echo $semaine->id; ?>" title="Afficher le Planning de la semaine du <?php echo $lundiFr; ?> au <?php echo $dimancheFr; ?>" data-site="<?php echo $site->id; ?>" data-lundi="<?php echo $lundi->date(); ?>" data-dimanche="<?php echo $dimanche->date(); ?>" data-tpl="<?php echo $planning->template; ?>" data-editable="<?php echo ($editable) ? '1' : '0'; ?>" data-periode="<?php echo $periode->id; ?>" data-semaine="<?php echo $semaine->id; ?>">
				<?php echo $semaine->libelle . " : $lundiFr au $dimancheFr" ?>
			    </a> 
                      <?php endif; ?>               
		</td>
                        
		<!-- PLANNING TEMPLATE -->
			<td<?php if($borderBottom): ?> style="border-bottom: solid 2px #999999"<?php endif; ?>>
			   
				    <?php if ($noTemplate): ?>
	    			    Aucun
				    <?php else: ?>
	    			    <a href="<?php echo $this->url('RHX/default', array('controller'	 => 'template', 'action'	 => 'edit', 'id'		 => $planning->template)); ?>" class="template-show <?php echo $classNow; ?>" data-site="<?php echo $site->id; ?>">
					    <?php echo $planning->template_libelle; ?>
	    			    </a>
				    <?php endif; ?>
				</td>

			<!-- BOUTON -->
				<td style="<?php if($borderBottom): ?>border-bottom: solid 2px #999999;"<?php endif; ?>text-align: center;">
				    <?php if ($now->isLT($lundi)): ?>
	    			    <!-- SEMAINE A VENIR -->
					<?php if ($noTemplate): ?>
					    <!-- AFFECTATION -->
					    <button class="btn btn-success addTemplate" data-template="0" data-semaine="<?php echo $semaine->id; ?>" data-periode="<?php echo $periode->id; ?>">
						Affecter un Planning Type
					    </button>
					<?php else: ?>
					    <!-- REMPLACEMENT -->
					    <!--button class="btn btn-info editTemplate" data-template="<?php echo $template; ?>" data-semaine="<?php echo $semaine->id; ?>" data-periode="<?php echo $periode->id; ?>" data-id="<?php echo $planning->pp_id; ?>">
						Remplacer le Planning Type
					    </button-->
					    <a href='#' class='btn btn-warning btn-small delete-planning' data-id='<?php echo $planning->pp_id; ?>' data-site='<?php echo $planning->site_id; ?>'><i class='icon-white icon-remove'></i></a>
					<?php endif; ?>
				    <?php elseif ($now->isLT($dimanche)): ?>
	    			    <!-- SEMAINE EN COURS -->
					<?php if ($noTemplate): ?>
					    <!-- AFFECTATION -->
					    <button class="btn btn-success addTemplate" data-template="0" data-semaine="<?php echo $semaine->id; ?>" data-periode="<?php echo $periode->id; ?>">
						Affecter un Planning Type
					    </button>
					<?php else: ?>
					    <!--button class="btn btn-inverse noEdit">
								<b>Non Modifiable</b>*
					    </button-->
							    <?php if ($lundi->isGT($septembre)): ?>
								<?php if ($noTemplate): ?>
								    <button class="btn btn-success addTemplate" data-template="0" data-semaine="<?php echo $semaine->id; ?>" data-periode="<?php echo $periode->id; ?>">
									Affecter un Planning Type
								    </button>
								<?php else: ?>
								    <!--button class="btn btn-info editTemplate" data-template="<?php echo $template; ?>" data-semaine="<?php echo $semaine->id; ?>" data-periode="<?php echo $periode->id; ?>" data-id="<?php echo $planning->pp_id; ?>">
									Remplacer le Planning Type
								    </button-->
								    <a href='#' class='btn btn-warning btn-small delete-planning' data-id='<?php echo $planning->pp_id; ?>' data-site='<?php echo $planning->site_id; ?>'><i class='icon-white icon-remove'></i></a>
								<?php endif; ?>
							    <?php else: ?>
		    					    <button class="btn btn-inverse noEdit">
		    						<b>Non Modifiable</b>*
		    					    </button>
							    <?php endif; ?>
					<?php endif; ?>
				    <?php else: ?>
					<?php if ($lundi->isGT($septembre)): ?>
					    <?php if ($noTemplate): ?>
		    			    <button class="btn btn-success addTemplate" data-template="0" data-semaine="<?php echo $semaine->id; ?>" data-periode="<?php echo $periode->id; ?>">
		    				Affecter un Planning Type
		    			    </button>
					    <?php else: ?>
		    			    <!--button class="btn btn-info editTemplate" data-template="<?php echo $template; ?>" data-semaine="<?php echo $semaine->id; ?>" data-periode="<?php echo $periode->id; ?>" data-id="<?php echo $planning->pp_id; ?>">
		    				Remplacer le Planning Type
		    			    </button-->
					    <a href='#' class='btn btn-warning btn-small delete-planning' data-id='<?php echo $planning->pp_id; ?>' data-site='<?php echo $planning->site_id; ?>'><i class='icon-white icon-remove'></i></a>
					    <?php endif; ?>
					<?php else: ?>
					    <button class="btn btn-inverse noEdit">
						Non Modifiable
					    </button>
					<?php endif; ?>
	    			    <!--button class="btn btn-inverse noEdit">
	    				Non Modifiable
	    			    </button-->
				    <?php endif; ?>
				</td>
		    </tr>
		    <tr>
			<?php $i++; ?>
		    <?php endforeach; ?>
    	    </tr>
	    <?php endforeach; ?>
	</tbody>
    </table>
</div>
<script type="text/javascript">
    // Identifiant du lien cliquer pour afficher le planning Semaine
    ID = '';
    // Bootstrap
    $(function() {
	// Edition des Plannings
	$('.planning-edit').click(function(event) {
	    event.preventDefault();
	    fullScreenLayout();
	    // chargement
	    site = $(this).attr('data-site');
	    lundi = $(this).attr('data-lundi');
	    dimanche = $(this).attr('data-dimanche');
	    template = $(this).attr('data-tpl');
	    editable = $(this).attr('data-editable');
	    periode = $(this).attr('data-periode');
	    data = $('#planningForm').serialize();
	    ID = $(this).attr('id');
	    //alert(ID);
	    ajax = $.ajax({
		type: 'post',
		url: '<?php echo $this->url('RHX/default', array('controller'	 => 'planning', 'action'	 => 'edit')) ?>',
		data: data + '&site_id=' + site + '&data-lundi=' + lundi + '&template=' + template + '&editable=' + editable + '&periode=' + periode
	    });
	    ajax.success(function(data) {
		$('#fullScreen').html("" + data).fadeIn('normal');
	    });
	    ajax.error(function() {
		alert('Erreur');
	    });
	});

	// Affichage des Planning Types (Template)
	$('.template-show').click(function(event) {
	    event.preventDefault();
	    fullScreenLayout();
	    //alert('Afficher Planning Type');
	    site = '<?php echo $site->id; ?>';
	    adresse = $(this).attr('href');
	    ajaxP = $.ajax({
		type: 'post',
		url: adresse,
		data: "&site_id=" + site + "&nohead=1&fullscreen=1"
	    });
	    ajaxP.success(function(data) {
		$('#fullScreen').html(data).fadeIn('normal');
		//alert('Chargé');
	    });
	    ajaxP.error(function() {
		alert('Erreur');
	    });
	});

	// Ajouter Template
	$('.addTemplate').click(function(event) {
	    event.preventDefault();
	    semaine = $(this).attr('data-semaine');
	    periode = $(this).attr('data-periode');
	    tpl = $(this).attr('data-template');
	    ajax = $.ajax({
		type: 'post',
		url: "<?php echo $this->url('RHX/default', array('controller'	 => 'planning', 'action'	 => 'selectTemplate')) ?>/<?php echo $site->id ?>",
		data: "&semaine=" + semaine + "&periode=" + periode + "&tpl=" + tpl
	    });
	    ajax.success(function(data) {
		$('#dialog1').html(data).dialog({
		    width: 500,
		    height: 400,
		    modal: true,
		    title: "Planning Type disponible pour le site <?php echo $site->libelle ?>",
		    autoOpen: true,
		    show: {effect: 'fade', duration: 1000},
		    hide: {effect: 'fade', duration: 700}
		});
	    });
	});

	// Modifier Template
	$('.editTemplate').click(function(event) {
	    event.preventDefault();
	    semaine = $(this).attr('data-semaine');
	    periode = $(this).attr('data-periode');
	    tpl = $(this).attr('data-template');
	    id = $(this).attr('data-id');

	    ajax = $.ajax({
		type: 'post',
		url: "<?php echo $this->url('RHX/default', array('controller'	 => 'planning', 'action'	 => 'selectChangeTemplate')) ?>/<?php echo $site->id ?>",
		data: "&semaine=" + semaine + "&periode=" + periode + "&tpl=" + tpl + "&pl_id=" + id
	    });
	    ajax.success(function(data) {
		$('#dialog1').html(data).dialog({
		    width: 500,
		    height: 400,
		    modal: true,
		    title: "Planning Type disponible pour le site <?php echo $site->libelle ?>",
		    autoOpen: true,
		    show: {effect: 'fade', duration: 1000},
		    hide: {effect: 'fade', duration: 700}
		});
	    });
	    ajax.error(function() {
		alert('error');
	    });
	});

	// Aucune Edition
	$('.noEdit').click(function(event) {
	    event.preventDefault();
	});
	
	// Effacer une semaine
	$('.delete-planning').click(function(e){
	    e.preventDefault();
	    if(!confirm("Voulez-vous effacer ce planning semaine ?")){
		return false;
	    }
	    id = $(this).attr('data-id');
	    site = $(this).attr('data-site');
	    ajax = $.ajax({
		type: 'post',
		url : '<?php echo $this->url('RHX/default', array('controller' => 'planning', 'action' => 'delete')) ?>/' + id + '/' + site
	    });
	    ajax.done(function(result){
		refresh();
	    });
	});
	
	// Mise à jour des sites
	var enCours = false;
	$('#btn-ps-update').click(function(e){
	    e.preventDefault();
	    if(enCours) return;
	    enCours = true;
	    site = $(this).attr('data-site');
	    $(this).removeClass('btn-primary').addClass('btn-inverse');
	    $('#btn-title').hide('normal');
	    $('#btn-loader').show('normal');
	    $ajax = $.ajax({
		type: 'post',
		url : '<?php echo $this->url('RHX/default', array('controller' => 'planning', 'action' => 'updateSitePlanning')) ?>',
		data: '&site='+  site
	    });
	    $ajax.done(function(result){
		if(result.length > 20)
		    showInfo(result);
		$('#btn-ps-update').removeClass('btn-inverse').addClass('btn-primary');
		$('#btn-title').show('normal');
		$('#btn-loader').hide('normal');
		enCours = false;
	    });
	});
    });
    // Masquage et Affichage
    function fullScreenLayout() {
	// masquage
	$('.left-content').fadeOut('slow');
	$('.right-content').fadeOut('slow');
	$('.brand-title').fadeOut('normal');
    }
    // Affichage et masquage
    function normalScreenLayout() {
	// Masquage
	$('#fullScreen').fadeOut('fast');
	// Affichage
	$('.left-content').fadeIn('fast');
	$('.right-content').fadeIn('fast');
	$('.brand-title').fadeIn('normal');
    }
</script>